package com.itheima.student.controller.Impl;

import com.itheima.count.controller.ICountController;
import com.itheima.domain.Student;
import com.itheima.excel.controller.Export;
import com.itheima.excel.controller.Import;
import com.itheima.factory.Factory;
import com.itheima.student.controller.IStudentController;
import com.itheima.student.service.IStudentService;
import com.itheima.student.service.Impl.StudentServiceImpl;

import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.ArrayList;
import java.util.Scanner;

public class StudentControllerImpl implements IStudentController {
    private Scanner sc = new Scanner(System.in);
    //工厂类
    private IStudentService studentService= Factory.getIStudentService();
    private ICountController iCountController=Factory.getICountController();

    public StudentControllerImpl() throws Exception {
    }


    /*
    学生系统界面
    */
    @Override
    public void start(String usename) throws Exception {
        //键盘录入
        readStudent();//进入系统读取文件
        lo:
        while (true) {
            //菜单搭建
            System.out.println("--------欢迎来到 <学生> 管理系统--------");
            System.out.println("<请输入您的选择: 1.添加学生  2.删除学生  3.修改学生  4.查看学生  5.查看报表>");
            System.out.println("<            6.导入excel        7.导出excel        8.退出            >");
            //创建分支
            String choose = sc.nextLine();
            switch (choose) {
                case "1":
                    addStudent();//增
                    break;
                case "2":
                    deleteStudent();//删
                    break;
                case "3":
                    updateStudent();//改
                    break;
                case "4":
                    findStudent();//查
                    break ;
                case "5":
                    iCountController.getReprot();//报表
                    break;
                case "6":
                    Import.importExcel();
                    break ;
                case "7":
                    Export.exportExcel(usename);
                    break ;
                case "8":
                    writeStudent();//退出时将数据写入文件
                    System.out.println("您的数据已经保存,感谢您的使用");//返回主菜单
                    break lo;
                default:
                    System.out.println("您的输入有误");
                    break;
            }
        }
    }


    /*
    添加
    */
    @Override
    public void addStudent() throws IOException {
        //判断学号
        //输入数据,已经改进学号不需要手动输入
        /*String sid = null;
        while (true) {
            System.out.println("请输入学生学号");
            sid = sc.nextLine();
            if (studentService.isExists(sid)) {
                System.out.println("该学号已经存在");
            } else {
                break;
            }
        }*/
        String sid = addSid();//自动生成学号
        Student student = getStudent(sid);//调用输入数据的方法

        int group = addGroup();
        student.setGroup(group);//调用方法生成组
        studentService.addStudent(student);
        System.out.println("添加成功");
        writeStudent();
    }

    //自动生成组,四个人一组,满了去下一组
    public int addGroup() {
        ArrayList<Student> students = studentService.findAllStudent();
        int group = 1;
        int count = 0;
        while (true) {
            count = 0;//统计组
            for (int i = 0; i < students.size(); i++) {
                if (students.get(i).getGroup() == group) {
                    count++;
                }
            }
            if (count >= 4) {
                group++;
            }else {
                break;
            }
        }
        return group;
    }

    //生成学号
    public String addSid(){
        int count=1;
        String sid="heima001";
        while (true) {
            if (studentService.isExists(sid)){
                if (count<10){
                    sid="heima00"+count;
                    count++;
                }else{
                    sid="haima0"+count;
                }
            }else {
                break;
            }
        }
        return sid;
    }


    /*
    删除学生界面
    */
    @Override
    public void deleteStudent() throws IOException {
        ArrayList<Student> students = studentService.findAllStudent();
        if (students.size() == 0) {
            System.out.println("暂时没有数据可删除,请先添加");
            return;
        }
        lo:
        while (true) {
            System.out.println("<----  删除界面 ---->");
            System.out.println("<请选择: 1.删除指定学号学生   2.删除化所有学生   3.退出>");
            String choose = sc.nextLine();
            switch (choose) {
                case "1":
                    deleteSidStudent();
                    break;
                case "2":
                    deleteAllStudent();
                    break;
                case "3":
                    break lo;
                default:
                    System.out.println("您的输入有误");
                    break;
            }
        }
    }

    //删除指定学号学生
    public void deleteSidStudent() throws IOException {
        String sid = getSid();
        studentService.deleteSidStudent(sid);
        System.out.println("删除成功");
        writeStudent();
    }

    //删除所有学生
    public void deleteAllStudent() throws IOException {
        studentService.deleteAllStudent();
        System.out.println("全部学生已经删除!");
        writeStudent();
    }


    /*
    修改界面
    */
    @Override
    public void updateStudent() throws IOException {
        //输入数据
        ArrayList<Student> students = studentService.findAllStudent();
        if (students.size() == 0) {
            System.out.println("暂时没有数据,请先添加");
            return;
        }
        String sid = getSid();  //调用学号存在的方法
        Student sidStudent = studentService.findSidStudent(sid);//查找学生修改某一属性
        lo:while (true) {
            System.out.println("<------- 修改学号为" + sid + "的学生 ------->");
            System.out.println("<请选择修改项:1.姓名  2.性别  3.生日  4.手机号>");
            System.out.println("<-----------5.组号  6.修改全部  7.退出------>");
            String choose = sc.nextLine();
            switch (choose) {
                case "1":
                    System.out.println("原名" + sidStudent.getName() + ",修改姓名");
                    String name = sc.nextLine();
                    sidStudent.setName(name);
                    System.out.println("姓名修改成功");
                    writeStudent();
                    break;
                case "2":
                    while (true) {
                        System.out.println("请修改该学生性别");
                        String sex = sc.nextLine();
                        if (studentService.sexIsTrue(sex)) {
                            sidStudent.setSex(sex);
                            System.out.println("修改成功");
                            writeStudent();
                            break;
                        } else {
                            System.out.println("格式错误,请重新输入");
                        }
                    }
                    break;
                case "3":
                    System.out.println("原生日" + sidStudent.getBirthday().toString() + ",修改生日");
                    LocalDate dateBirthday = getLocalDate();//调用录入生日并解析生日的方法
                    sidStudent.setBirthday(dateBirthday);
                    System.out.println("生日修改成功");
                    writeStudent();
                    break;
                case "4":
                    System.out.println("原手机号" + sidStudent.getPhone() + ",修改手机号");
                    String phone = getPhoneLengh();//先判断手机号格式
                    while (true) {                 //再判断手机号是否存在
                        if (studentService.phoneIsExists(phone)) {
                            System.out.println("该手机号已经被绑定,请重新输入");
                        } else {
                            System.out.println("手机号修改成功");
                            writeStudent();
                            break;
                        }
                    }
                    break;
                case "5":
                    updataGroup(sidStudent);//调用方法修改组,传按学号查找的学生参数
                    break;
                case "6":       //修改全部
                    Student student = getStudent(sid);
                    updataGroup(student);   //调用方法修改该学生的组
                    studentService.updateStudent(sid, student);
                    System.out.println("修改成功");
                    writeStudent();
                    break;
                case "7":
                    System.out.println("修改结束,感谢您的使用");
                    break lo;
                default:
                    break;
            }
        }
    }

    //修改组号,解决异常
    public void updataGroup(Student student) {
        System.out.println("请输入修改的学生组号");
        while (true) {
            try {
                String group = sc.nextLine();
                student.setGroup(Integer.parseInt(group));
                break;
            } catch (NumberFormatException e) {
                System.out.println("不是数字,输入有误,重新输入");
            }
        }
    }


    /*
    查询界面
    */
    @Override
    public void findStudent() {
        ArrayList<Student> students = studentService.findAllStudent();
        if (students.size() == 0) {
            System.out.println("暂时没有数据,请先添加");
            return;
        }
        lo:
        while (true) {
            System.out.println("<-----------------  这里是查询界面  -------------------->");
            System.out.println("<请选择: 1.按学号查   2.按姓名查   3.按年龄查   4.按手机号查>");
            System.out.println("<-----   5按组号查      6.查看全部      7.退出    ------->");
            String choose = sc.nextLine();
            switch (choose) {
                case "1":
                    findSidStudent();
                    break;
                case "2":
                    findNameStudent();
                    break;
                case "3":
                    findAgeStudent();
                    break;
                case "4":
                    findPhoneStudent();
                    break;
                case "5":
                    findGroupStudent();
                    break;
                case "6":
                    findAllStudent();
                    break;
                case "7":
                    break lo;
                default:
                    System.out.println("您的输入有误");
                    break;
            }
        }
    }

    //查询所有
    public void findAllStudent() {
        ArrayList<Student> students = studentService.findAllStudent();
        System.out.println("所有学生信息如下");
        System.out.println("学号\t\t\t组号\t姓名\t年龄\t生日\t\t\t手机");
        students.forEach(student -> getPrintln(student));
    }

    //按学号查询
    public void findSidStudent() {
        System.out.println("请输入学生学号");
        String sid = sc.nextLine();
        if (studentService.isExists(sid)) {
            Student student = studentService.findSidStudent(sid);
            System.out.println("学号为" + sid + "学生信息如下:");
            System.out.println("学号\t\t\t组号\t姓名\t年龄\t生日\t\t\t手机");
            getPrintln(student);//打印
        } else {
            System.out.println("不存在学号为" + sid + "学生信息");
        }
    }

    //按姓名查询
    public void findNameStudent() {
        System.out.println("请输入学生姓名");
        String name = sc.nextLine();
        ArrayList<Student> students = studentService.findNameStudent(name);
        if (students.size() == 0) {
            System.out.println("不存在姓名为" + name + "的学生信息");
            return;
        }
        System.out.println("姓名为" + name + "的学生信息如下");
        System.out.println("学号\t\t\t组号\t姓名\t年龄\t生日\t\t\t手机");
        students.forEach(student -> getPrintln(student));//打印
    }

    //按年龄查询
    public void findAgeStudent() {
        System.out.println("请输入学生年龄");
        int age = sc.nextInt();
        ArrayList<Student> students = studentService.findAgeStudent(age);
        if (students.size() == 0) {
            System.out.println("不存在年龄为" + age + "的学生信息");
            return;
        }
        System.out.println("年龄为" + age + "的学生信息如下");
        System.out.println("学号\t\t\t组号\t姓名\t年龄\t生日\t\t\t手机");
        students.forEach(student -> getPrintln(student));//打印
    }

    //按手机号查询
    public void findPhoneStudent() {
        System.out.println("请输入学生手机号");
        String phone = sc.nextLine();
        ArrayList<Student> students = studentService.findPhoneStudent(phone);
        if (students.size() == 0) {
            System.out.println("不存在手机号为" + phone + "的学生信息");
            return;
        }
        System.out.println("手机号为" + phone + "的学生信息如下");
        System.out.println("学号\t\t\t组号\t姓名\t年龄\t生日\t\t\t手机");
        students.forEach(student -> getPrintln(student));//打印
    }

    //按组查询
    public void findGroupStudent() {
        System.out.println("请输入学生组号");
        int group = sc.nextInt();
        ArrayList<Student> students = studentService.findGroupStudent(group);
        if (students.size() == 0) {
            System.out.println("这个组不存在");
            return;
        }
        System.out.println("第" + group + "的学生信息如下");
        System.out.println("学号\t\t\t组号\t姓名\t年龄\t生日\t\t\t手机");
        students.forEach(student -> getPrintln(student));
    }

    //打印学生信息
    private void getPrintln(Student student) {
        System.out.println(student.getSid() + "\t" + student.getGroup() + "组\t" + student.getName() + "\t" + student.getAge() + "\t" + student.getBirthday() + "\t" + student.getPhone());
    }


    //写入文件
    @Override
    public void writeStudent() throws IOException {
        studentService.writeStudent();
    }


    //将文件读到集合
    @Override
    public void readStudent() throws Exception {
        studentService.readStudent();
    }


    //获取学号,如果学号不存在,将无法跳出循环
    private String getSid() {
        String sid = null;
        while (true) {
            System.out.println("请输入学生学号");
            sid = sc.nextLine();
            if (studentService.isExists(sid)) {
                break;
            } else {
                System.out.println("该学号不存在");
            }
        }
        return sid;
    }

    //增加和修改添加数据相同的代码,抽取成一个方法,获取学生对象
    private Student getStudent(String sid) {
        System.out.println("请输入学生姓名");
        String name = sc.nextLine();
        String sex = null;
        while (true) {
            System.out.println("请修改该学生性别");
            sex = sc.nextLine();
            if (studentService.sexIsTrue(sex)) {
                break;
            } else {
                System.out.println("格式错误,请重新输入");
            }
        }
        LocalDate dateBirthday = getLocalDate();
        //判断手机号的格式
        String phone = null;
        while (true) {
            System.out.println("请输入学生手机号");
            phone = getPhoneLengh();
            if (studentService.phoneIsExists(phone)) {
                System.out.println(phone + ",该手机号已经被绑定");
            } else {
                break;
            }
        }
        return new Student(sid, name, sex, dateBirthday, phone);
    }

    //判断手机号的格式,不是纯数字或者11位会一直循环
    private String getPhoneLengh() {
        String phone = null;
        while (true) {
            phone = sc.nextLine();
            if (studentService.phoneIsTrue(phone)) {
                break;
            } else {
                System.out.println("格式错误");
            }
        }
        return phone;
    }

    //录入生日,解析字符串生日为LocalDate类型,输入格式不能错误
    private LocalDate getLocalDate() {
        LocalDate dateBirthday = null;
        while (true) {
            System.out.println("请输入学生生日(yyyy-MM-dd)");
            String birthday = sc.nextLine();
            //解析生日,创建对象
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            try {
                dateBirthday = LocalDate.parse(birthday, formatter);
                break;
            } catch (DateTimeParseException e) {
                System.out.println("格式错误,请输入正确格式");
            }
        }
        return dateBirthday;
    }

}
